'use strict'

//import { RSA_NO_PADDING } from "constants";


//const uuid = require('uuid');
import {verifyToken} from "../lib/utljwt"

module.exports = () => {
    return async function userInterceptor(ctx, next) {
        let authToken = ctx.header.authorization // 获取header里的authorization
        if (authToken) {
            //authToken = authToken.substring(7)
            const res = verifyToken(authToken) // 解密获取的Token

            if ( res && res.isTimeout === false && res.username != "") {
                ctx.usernameByToken = res.username;
                await next()
                return;
            } else if (res?.isTimeout) {
                ctx.body = { error: { code: 50012, message: '登录状态已过期' } }
                return
            }
        }
        ctx.body = { error: { code: 50008, message: '请登陆后再进行操作' } }
    }
}



